import { getRandom } from "../utils";
import { SquareGroup } from "./SquareGroup";
import { Point } from "./types";

/**
 *  口
 * 口口口
 */
class Shapet extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: -1 },
        { x: -1, y: 0 },
        { x: 0, y: 0 },
        { x: 1, y: 0 }
      ],
      _centerPoint,
      _color
    );
  }
}

/**
 * 不用旋转
 * 口口
 * 口口
 */
class ShapeTian extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: 1 },
        { x: 1, y: 1 }
      ],
      _centerPoint,
      _color
    );
  }
  /**
   * 重写父类方法：旋转规则有不同
   * 口口
   * 口口  不用旋转：形状不变
   */
  rotate() {
    return;
  }
}

/**
 * 旋转只有两种状态
 *  口口
 * 口口
 */
class Shapes extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 1, y: 0 },
        { x: 0, y: 1 },
        { x: -1, y: 1 }
      ],
      _centerPoint,
      _color
    );
  }
  /**
   * 重写父类方法：该形状旋转时只有两种情况
   */
  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

/**
 * 旋转只有两种状态
 * 口口
 *  口口
 */
class ShapeS extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: -1, y: 0 },
        { x: 0, y: 1 },
        { x: 1, y: 1 }
      ],
      _centerPoint,
      _color
    );
  }
  /**
   * 重写父类方法：该形状旋转时只有两种情况
   */
  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

/**
 * 口
 * 口
 * 口口
 */
class Shapel extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 0, y: -1 },
        { x: 0, y: 1 },
        { x: 1, y: 1 }
      ],
      _centerPoint,
      _color
    );
  }
}

/**
 *   口
 *   口
 * 口口
 */
class ShapeL extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 0, y: -1 },
        { x: 0, y: 1 },
        { x: -1, y: 1 }
      ],
      _centerPoint,
      _color
    );
  }
}

/**
 * 旋转只有两种状态
 * 口
 * 口
 * 口
 * 口
 */
class ShapeI extends SquareGroup {
  constructor(_centerPoint: Point, _color: string) {
    super(
      [
        { x: 0, y: 0 },
        { x: 0, y: -1 },
        { x: 0, y: 1 },
        { x: 0, y: 2 }
      ],
      _centerPoint,
      _color
    );
  }
  /**
   * 重写父类方法：该形状旋转时只有两种情况
   */
  rotate(): void {
    super.rotate();
    this.isClock = !this.isClock;
  }
}

// 俄罗斯方块：Tetris
export const tetris = [ShapeI, ShapeL, ShapeS, ShapeTian, Shapel, Shapes, Shapet];
export const colors = ["red", "blue", "green", ""];

export function createTeris(centerPoint: Point): SquareGroup {
  const index = getRandom(0, tetris.length);
  const Ter = tetris[index];
  const color = `rgb(${getRandom(0, 256)}, ${getRandom(0, 256)}, ${getRandom(0, 256)})`;

  return new Ter(centerPoint, color);
  // return new SquareGroup(ter, centerPoint, color);
}
